﻿#include <QTcpSocket>
#include "RecognizeDevice.h"
#include "../mainwindow.h"

RecognizeDevice::RecognizeDevice(QObject *parent)
    : Device{parent}
    , m_pDescription((Description*)parent)
{

}

RecognizeDevice::~RecognizeDevice(){
    uninitDevice();
}

int RecognizeDevice::initDevice() {
     return 0;
}

void RecognizeDevice::uninitDevice() {
}

QString RecognizeDevice::getClassName()
{
    return this->metaObject()->className();
}

void RecognizeDevice::writeXML(QXmlStreamWriter &writer)
{
    // 写入当前操作的信息
    writer.writeStartElement("RecognizeDevice");
    writer.writeAttribute(QStringLiteral("id"), m_strID);

    writer.writeEndElement(); // 结束当前操作的元素
}

void RecognizeDevice::readFromXML(QXmlStreamReader &reader)
{
    QXmlStreamAttributes attributes = reader.attributes();
    if (attributes.hasAttribute("id")) {
        m_strID = attributes.value("id").toString();
    }

    QXmlStreamReader::TokenType token = reader.readNext();
    while (!(reader.tokenType() == QXmlStreamReader::EndElement
             && reader.name() == QStringLiteral("RecognizeDevice"))) {
        reader.readNext();
    }
}

void RecognizeDevice::writeData(const QVariant &data) {
    QMap<QString,QVariant> map = data.toMap();
    int imageNum = ((QVariant)map["content"]).toInt();
    qDebug() << "RecognizeDevice content = " << imageNum;


    char* pSrcBuffer = MainWindow::instance()->GetImageSrcBuffer(imageNum);

    cv::Mat imageSrc(IMAGE_HEIGHT,IMAGE_WIDTH,CV_8UC3);
    memcpy(imageSrc.data,pSrcBuffer,IMAGE_WIDTH * IMAGE_HEIGHT * 3);
    cv::Mat imageDst = imageSrc.clone();

    qint64 CurrentMEpoch1 = QDateTime::currentMSecsSinceEpoch();
    //std::vector<Detection> result = m_objDefectRecognizer.RecognizeDefect(imageSrc);
    std::vector<Detection> result = m_objDefectRecognizer.RecognizePartsDefect(imageSrc);
    qint64 CurrentMEpoch2 = QDateTime::currentMSecsSinceEpoch();
    QString elapse = QString::number(CurrentMEpoch2 - CurrentMEpoch1);
    qDebug() << elapse << "ms";

}
